﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace CC98.HtmlParser
{
	/// <summary>
	/// 解析主页的内部解析器。
	/// </summary>
	internal class HomePageParser : Parser<HomePageInfo>
	{
		/// <summary>
		/// 从 HTML 中提取自定义版面列表。
		/// </summary>
		/// <param name="rawHtml">要分析的原始 HTML。</param>
		/// <param name="info">保存分析结果的对象。</param>
		private static void ParseCustomBoards(string rawHtml, HomePageInfo info)
		{
			const string _Pattern = @"var customboards='(?<list>.*)'";

			var m = rawHtml.Match(_Pattern);

			if (m.Success)
			{
				var list = m.Groups["list"].Value;

				var ids = list.Split(',');

				info.CustomBoardIds = (from i in ids
									   select i.ToInt32()).ToArray();
			}
		}

		/// <summary>
		/// 从 HTML 中提取论坛统计信息。
		/// </summary>
		/// <param name="rawHtml">要分析的原始 HTML。</param>
		/// <param name="info">保存分析结果的对象。</param>
		private static void ParseCounts(string rawHtml, HomePageInfo info)
		{
			// 分析模式1，用于获得会员数、主题数和帖子数。
			const string _Pattern1 = @"论坛共有 <b>(?<user>\d+)</b> 位注册会员 , 主题总数：<b>(?<topic>\d+)</b> , 帖子总数：<b>(?<post>\d+)</b>";
			// 分析模式2，用于获得今日帖数，历史记录数目和日期。
			const string _Pattern2 = @"今日贴数.*?<b>(?<today>\d+)</b>.*, 历史纪录：<b>(?<record>\d+)</b>, 发生于.*?(?<year>\d+)\.(?<month>\d+)\.(?<day>\d+)";

			var m = rawHtml.Match(_Pattern1);

			if (m.Success)
			{
				info.UserCount = m.Groups["user"].Value.ToInt32();
				info.TopicCount = m.Groups["topic"].Value.ToInt32();
				info.PostCount = m.Groups["post"].Value.ToInt32();
			}

			m = rawHtml.Match(_Pattern2);

			if (m.Success)
			{
				info.TopicToday = m.Groups["today"].Value.ToInt32();
				info.TopicRecordCount = m.Groups["record"].Value.ToInt32();

				var year = m.Groups["year"].Value.ToInt32();
				var month = m.Groups["month"].Value.ToInt32();
				var day = m.Groups["day"].Value.ToInt32();

				info.TopicRecordDate = new DateTime(year, month, day);
			}

		}

		private static void ParseRootBoards(string rawHtml, HomePageInfo info)
		{
			var pattern = @"<a href=list\.asp\?boardid=(?<id>\d+?)><font color=\#000066>(?<name>.*?)</font></a>.*?有(?<subcount>\d+?)个下属论坛.*?<td width=""50%"" title=""(?<desc>.*?)<br>版主：<a href=dispuser\.asp\?name=(?<manager>.*?) target=_blank>.*?今日：<font color=\#FF0000>(?<today>\d+?)</font>.*?发贴：(?<total>\d+?)</td>";

			var ms = rawHtml.MatchAll(pattern);

			foreach (Match m in ms)
			{
				var binfo = new HomePageBoardInfo
				{
					Id = m.Groups["id"].Value.ToInt32(),
					Name = m.Groups["name"].Value,
					Description = m.Groups["desc"].Value,
					SubBoardCount = m.Groups["subcount"].Value.ToInt32(),
					Manager = m.Groups["manager"].Value,
					TopicToday = m.Groups["today"].Value.ToInt32(),
					TopicCount = m.Groups["total"].Value.ToInt32()
				};

				info.RootBoards.Add(binfo);
			}
		}

		/// <summary>
		/// 分析 HTML 并获得主页的相关信息。
		/// </summary>
		/// <param name="rawHtml">要分析的 HTML。</param>
		/// <returns>分析后的结果。</returns>
		public override HomePageInfo Parse(string rawHtml)
		{
			var result = new HomePageInfo();

			ParseCustomBoards(rawHtml, result);
			ParseCounts(rawHtml, result);
			ParseRootBoards(rawHtml, result);

			return result;
		}
	}
}
